﻿using System;
using System.IO;
using System.Windows.Forms;

namespace UserManage
{
    public partial class Login : Form
    {
        int minLevel = 0;
        private int userNum = 0;
        private string userLevelName = "--";
        private string userName = "";
        private bool changePassword = false;
        private string[] gcsuser = new string[20];
        private string[] czyuser = new string[20];
        private System.Timers.Timer TimerPLC;
        public Login(int minLevel = 0)
        {
            this.minLevel = minLevel;
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            tbId.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CheckPwd();
        }

        private void CheckPwd()
        {
            string UserCode = tbId.Text.Trim();
            if (string.IsNullOrEmpty(UserCode))
            {
                label1.Text = "用户输入错误";
                return;
            }
            if (Authentication.Login(UserCode) == null)
            {
                label1.Text = "登录失败，用户错误";
                return;
            }
            Close();
        }

        public int UserNum
        {
            get { return userNum; }
        }

        public string UserName
        {
            get { return userName; }
        }

        public string UserLevelName
        {
            get { return userLevelName; }
        }

        private void TimerPLC_Elapsed(object sender, EventArgs e)
        {
            //return;
            TimerPLC.Stop();
            TimerPLC.Dispose();
            label1.Text = "请使用刷卡机";
            //MessageBox.Show("请使用刷卡机");
            if (tbId.Text.Trim() != "x")
            {
                userNum = 0;
            }

            if ((tbId.Text.Trim() != "x") && !File.Exists("debuguser.txt"))
            {
                this.Close();
            }

            return;
        }
        private void users_TextChanged(object sender, EventArgs e)
        {
            if (TimerPLC != null)
            {
                TimerPLC.Stop();
                TimerPLC.Dispose();
            }
            if (tbId.Text.Length < 6)
            {
                TimerPLC = new System.Timers.Timer();
                TimerPLC.Interval = 100;
                TimerPLC.Elapsed += TimerPLC_Elapsed;
                TimerPLC.Start();
                TimerPLC.AutoReset = true;
            }
        }

        private void users_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\u0016')
            {
                MessageBox.Show("禁止使用复制黏贴");
                userNum = 0;
                this.Close();
                return;
            }
            if (e.KeyChar == '\r')
            {
                CheckPwd();
                //string temp1 = gcsuser.Where(a => (a != null && a == tbId.Text.Trim('\r').Trim('\n'))).FirstOrDefault();
                //if (temp1 != null && temp1 == tbId.Text.Trim('\r').Trim('\n'))
                //{
                //    userNum = 2;
                //    this.Close();
                //    return;
                //}
                //string temp2 = czyuser.Where(a => (a != null && a == tbId.Text.Trim('\r').Trim('\n'))).FirstOrDefault();
                //if (temp2 != null && temp2 == tbId.Text.Trim('\r').Trim('\n'))
                //{
                //    userNum = 1;
                //    this.Close();
                //    return;
                //}
                //else
                //{
                //    userNum = 0;
                //    MessageBox.Show("用户输入错误");
                //}
            }

        }

        private void users_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                MessageBox.Show("禁止使用复制黏贴");
                userNum = 0;
                this.Close();
                return;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            CheckPwd();
        }
    }
}
